# 
# @Author: Cjuicy 
# @Date: 2022-03-21 20:25:16 
# @Last Modified by:   Cjuicys 
# @Last Modified time: 2022-03-21 20:25:16 
# 
'''
存放价钱的读函数
'''
import csv
from matplotlib.pyplot import axis
import numpy as np
import pandas as pd 

from pip import main


# 读取csv文件获得价钱信息  
#注意csv不能有多余空行，最下面只能有一个空行（否则会报错）
def get_price_info():
    price_info = {}
    names_info = {}
    with open('foodInfo.csv', 'r',encoding="utf-8") as csvfile: # 此目录即是当前项目根目录
        spamreader = csv.reader(csvfile)
        # 逐行遍历csv文件,按照字典存储用户名与密码
        for row in spamreader:
            price_info[row[0]] = row[1]
            names_info[row[0]] = row[2]
    return price_info,names_info

    

#读取csv文件获得价钱信息
def get_food_info():
    arr_all=[]
    with open('foodInfo.csv', 'r',encoding="utf-8") as csvfile: # 此目录即是当前项目根目录
        spamreader = csv.reader(csvfile)
        # 逐行遍历csv文件,按照字典存储用户名与密码
        for row in spamreader:
            arr_all.append(row)
    arr_all = np.array(arr_all)
    return arr_all, arr_all.shape[0]

'''对foodInfo.csv增加操作'''
def add_arr_food_info_csv(new_arr):
    with open('foodInfo.csv', 'a+',newline="" ,encoding="utf-8") as csvfile: # 此目录即是当前项目根目录
        wf = csv.writer(csvfile)
        wf.writerow(new_arr)
    return 0

'''对foodInfo.csv删除操作'''
def delete_arr_food_info_csv(num):
    df = pd.read_csv("foodInfo.csv",header=None,encoding="utf-8")
    num = int(num)
    df = df.drop(num)
    df.to_csv("foodInfo.csv",index=0,header=None,encoding="utf-8")
    return 0

'''对foodInfo.csv修改操作'''
def alter_arr_food_info_csv(num,arr):
    arr = np.array(arr)


    data = pd.read_csv("foodInfo.csv",header=None,encoding="utf-8")
    if len(arr[0])!=0:
        data.iat[num,0] = arr[0]                    #替换csv指定位置替换        应为没有标题，所以都是用编号为索引
    
    if len(arr[1])!=0:
        data.iat[num,1] = arr[1]
    
    if len(arr[2])!=0:
        data.iat[num,2] = arr[2]
    data.to_csv("foodInfo.csv",index=0,header=None,encoding="utf-8")
    return 0





    return 0

# if __name__ == '__main__':
#     get_price_info()